'use strict';

/**
 * Created by zhu on 14/12/21.
 */
angular.module('common').directive('ngPagination', ['PageConstant', function (PageConstant) {
    function Paginator() {
        this.countPerPage = PageConstant.countPerPage;
        this.offset = 0;
        this.link = function () {
            var self = this;

            return function ($scope, $element, $attrs) {
                $scope.$watch('totalCount', function (value) {
                    if (value) {
                        $($element).pagination({
                            currentPage: self.offset/self.countPerPage + 1,
                            items: value,
                            itemsOnPage: self.countPerPage,
                            prevText: '上一页',
                            nextText: '下一页',
                            onPageClick: self.pageClick($scope)
                        });
                    }
                }, true);
            }
        }
    }

    Paginator.prototype.pageClick = function ($scope) {
        return function (pageNumber, event) {
            event.preventDefault();
            $scope.query(pageNumber);
        }
    }

    return {
        link: new Paginator().link()
    }
}]);
